package com.ruoyi.jjyh.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jjyh.mapper.JjyhCarMapper;
import com.ruoyi.jjyh.domain.JjyhCar;
import com.ruoyi.jjyh.service.IJjyhCarService;
import com.ruoyi.common.core.text.Convert;

/**
 * 购物车Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-11-26
 */
@Service
public class JjyhCarServiceImpl implements IJjyhCarService 
{
    @Autowired
    private JjyhCarMapper jjyhCarMapper;

    /**
     * 查询购物车
     * 
     * @param id 购物车ID
     * @return 购物车
     */
    @Override
    public JjyhCar selectJjyhCarById(String id)
    {
        return jjyhCarMapper.selectJjyhCarById(id);
    }

    /**
     * 查询购物车列表
     * 
     * @param jjyhCar 购物车
     * @return 购物车
     */
    @Override
    public List<JjyhCar> selectJjyhCarList(JjyhCar jjyhCar)
    {
        return jjyhCarMapper.selectJjyhCarList(jjyhCar);
    }

    /**
     * 新增购物车
     * 
     * @param jjyhCar 购物车
     * @return 结果
     */
    @Override
    public int insertJjyhCar(JjyhCar jjyhCar)
    {
        jjyhCar.setCreateTime(DateUtils.getNowDate());
        return jjyhCarMapper.insertJjyhCar(jjyhCar);
    }

    /**
     * 修改购物车
     * 
     * @param jjyhCar 购物车
     * @return 结果
     */
    @Override
    public int updateJjyhCar(JjyhCar jjyhCar)
    {
        return jjyhCarMapper.updateJjyhCar(jjyhCar);
    }

    /**
     * 删除购物车对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJjyhCarByIds(String ids)
    {
        return jjyhCarMapper.deleteJjyhCarByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除购物车信息
     * 
     * @param id 购物车ID
     * @return 结果
     */
    @Override
    public int deleteJjyhCarById(String id)
    {
        return jjyhCarMapper.deleteJjyhCarById(id);
    }

    public List<Map<String, Object>> selectCarByUserId(String userId){
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> carList =  jjyhCarMapper.selectCarByUserId(userId);
        String goodsUserIds = "";
        for(Map<String, Object> a:carList){
            Map<String, Object> car = new HashMap<>();
            if(!goodsUserIds.contains(a.get("goodsUserId").toString())){
                car.put("goodsUserId", a.get("goodsUserId"));
                car.put("nickname", a.get("nickname"));
                List<Map<String, Object>> listb = new ArrayList<>();
                for(Map<String, Object> b:carList){
                    if(a.get("goodsUserId").equals(b.get("goodsUserId"))){
                        listb.add(b);
                    }
                }
                car.put("goodsList", listb);
                list.add(car);
                goodsUserIds += a.get("goodsUserId") + ",";
            }

        }
        return list;
    }
}
